/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package topin.facecat.FaceCat.UI;
import topin.facecat.FaceCat.Service.PriceData;
import topin.facecat.FaceCat.Service.PriceDataServiceClient;
import topin.facecat.FaceCat.Service.Security;
import facecat.topin.core.*;
import facecat.topin.service.*;
import facecat.topin.grid.*;
import java.util.*;

/*
 * 股票列表
 */
public class SecurityList extends  FCGrid implements FCListenerMessageCallBack, FCInvokeEventCallBack {
    public SecurityList()
    {
        setFont(new FCFont("Default", 14));
    }

    public int m_requestID = -1;

    /*
     *绑定代码
     *@param code
     */
    public void bindCodes(ArrayList<Security> codes)
    {
        m_rowsMap.clear();
        clearRows();
        update();
        getSelectedRows().clear();
        beginUpdate();
        for(int i = 0; i < codes.size(); i++){
            Security security = codes.get(i);
            if (security.m_name.length() > 0)
            {
                GridRowEx row = new GridRowEx();
                addRow(row);
                row.setHeight(25);
                String showCode = security.m_code.substring(0, security.m_code.indexOf("."));
                row.setTag(security.m_code);
                row.addCell("colP0", new FCGridNoCell());
                row.addCell("colP1", new FCGridStringCell(showCode));
                row.addCell("colP2", new FCGridStringCell(security.m_name));
                FCGridDoubleCellEx priceCell = new FCGridDoubleCellEx();
                priceCell.m_underLine = true;
                row.addCell("colP3", priceCell);
                row.addCell("colP4", new FCGridPercentCell());
                row.addCell("colP5", new FCGridDoubleCellEx());
                row.addCell("colP9", new FCGridUnitDoubleCell());
                row.addCell("colP10", new FCGridUnitDoubleCell());
                row.addCell("colP11", new FCGridDoubleCellEx());
                row.addCell("colP12", new FCGridPECell());
                row.addCell("colP13", new FCGridPercentCell());
                row.addCell("colP14", new FCGridPercentCell());
                row.addCell("colP15", new FCGridUnitLongCell());
                row.addCell("colP16", new FCGridUnitLongCell());
                row.addCell("colP17", new FCGridStringCell());
                row.addCell("colP18", new FCGridDoubleCellEx());
                row.addCell("colP19", new FCGridDoubleCellEx());
                row.addCell("colP20", new FCGridDoubleCellEx());
                row.addCell("colP21", new FCGridPercentCell());
                row.addCell("colP22", new FCGridPercentCell());
                row.addCell("colP23", new FCGridPercentCell());
                row.addCell("colP24", new FCGridDoubleCell());
                m_rowsMap.put(security.m_code, row);
                for (int j = 0; j < row.m_cells.size(); j++)
                {
                    FCGridCell cell = row.m_cells.get(j);
                    FCGridCellStyle cellStyle = new FCGridCellStyle();
                    cell.setStyle(cellStyle);
                    if (j == 0 || j == 2)
                    {
                        cellStyle.m_textColor = MyColor.USERCOLOR217;
                    }
                    else
                    {
                        cellStyle.m_textColor = MyColor.USERCOLOR218;
                    }
                }
            }
        }
        endUpdate();
        invalidate();
    }

    private HashMap<String, FCGridRow> m_rowsMap = new HashMap<String, FCGridRow>();

    /*
     * 跨线程调用
     *@param eventName
     *@param sender
     *@param args
     *@param invoke
     */
    public void callInvokeEvent(String eventName, Object sender, Object args, Object invoke)
    {
        if (args != null)
        {
            FCMessage message = (FCMessage)((args instanceof FCMessage) ? args : null);
            if (message != null)
            {
                if (message.m_serviceID == PriceDataServiceClient.SERVICEID)
                {
                    if (message.m_functionID == PriceDataServiceClient.FUNCTION_NEWDATA)
                    {
                        HashMap<String, Integer> columnsMap = new HashMap<String, Integer>();
                        for (int i = 0; i < m_columns.size(); i++)
                        {
                            FCGridColumn gridColumn = m_columns.get(i);
                            columnsMap.put(gridColumn.getName(), gridColumn.getIndex());
                        }
                        ArrayList<Integer> columnIndex = new ArrayList<Integer>();
                        columnIndex.add(columnsMap.get("colP3"));
                        columnIndex.add(columnsMap.get("colP4"));
                        columnIndex.add(columnsMap.get("colP5"));
                        columnIndex.add(columnsMap.get("colP9"));
                        columnIndex.add(columnsMap.get("colP10"));
                        columnIndex.add(columnsMap.get("colP11"));
                        columnIndex.add(columnsMap.get("colP12"));
                        columnIndex.add(columnsMap.get("colP13"));
                        columnIndex.add(columnsMap.get("colP14"));
                        columnIndex.add(columnsMap.get("colP15"));
                        columnIndex.add(columnsMap.get("colP16"));
                        columnIndex.add(columnsMap.get("colP17"));
                        columnIndex.add(columnsMap.get("colP18"));
                        columnIndex.add(columnsMap.get("colP19"));
                        columnIndex.add(columnsMap.get("colP20"));
                        columnIndex.add(columnsMap.get("colP21"));
                        columnIndex.add(columnsMap.get("colP22"));
                        columnIndex.add(columnsMap.get("colP23"));
                        columnIndex.add(columnsMap.get("colP24"));
                        ArrayList<PriceData> priceDatas = PriceDataServiceClient.getPriceDatas(message.m_body, message.m_bodyLength);
                        for (int i = 0; i < priceDatas.size(); i++)
                        {
                            PriceData priceData = priceDatas.get(i);
                            String code = priceData.m_code;
                            if (m_rowsMap.containsKey(code))
                            {
                                FCGridRow row = m_rowsMap.get(code);
                                double lastClose = priceData.m_lastClose;
                                double diff = 0, diffRange = 0;
                                double close = priceData.m_close;
                                if (close > 0)
                                {
                                    diff = close - priceData.m_lastClose;
                                }
                                if (close > 0 && priceData.m_lastClose != 0)
                                {
                                    diffRange = diff / priceData.m_lastClose;
                                }
                                FCGridCell cell3 = row.getCell(columnIndex.get(0));
                                cell3.setDouble(close);
                                if (close > 0 && close == priceData.m_upperLimit)
                                {
                                    cell3.m_style.m_backColor = FCColor.rgb(255, 82, 82);
                                    cell3.m_style.m_textColor = FCColor.Back;
                                }
                                else if (close > 0 && close == priceData.m_lowerLimit)
                                {
                                    cell3.m_style.m_backColor = FCColor.rgb(82, 255, 82);
                                    cell3.m_style.m_textColor = FCColor.Back;
                                }
                                else
                                {
                                    cell3.m_style.m_backColor = FCColor.None;
                                    cell3.m_style.m_textColor = FCDraw.getPriceColor(close, lastClose);
                                }

                                FCGridCell cell4 = row.getCell(columnIndex.get(1));
                                cell4.setDouble(diffRange);
                                cell4.m_style.m_textColor = FCDraw.getPriceColor(close, lastClose);

                                FCGridCell cell5 = row.getCell(columnIndex.get(2));
                                cell5.setDouble(diff);
                                cell5.m_style.m_textColor = FCDraw.getPriceColor(close, lastClose);

                                FCGridCell cell9 = row.getCell(columnIndex.get(3));
                                cell9.setDouble(priceData.m_volume / 100);

                                FCGridCell cell10 = row.getCell(columnIndex.get(4));
                                cell10.setDouble(priceData.m_amount);

                                FCGridCell cell11 = row.getCell(columnIndex.get(5));
                                cell11.setDouble(priceData.m_ratio);

                                FCGridCell cell12 = row.getCell(columnIndex.get(6));
                                cell12.setDouble(priceData.m_pe);

                                double range = 0;
                                if (lastClose != 0)
                                {
                                    range = (priceData.m_high - lastClose) / lastClose - (priceData.m_low - lastClose) / lastClose;
                                }
                                FCGridCell cell13 = row.getCell(columnIndex.get(7));
                                cell13.setDouble(range);
                                FCGridCell cell14 = row.getCell(columnIndex.get(8));
                                cell14.setDouble(priceData.m_turnoverRate / 100);
                                FCGridCell cell15 = row.getCell(columnIndex.get(9));
                                cell15.setDouble(priceData.m_marketValue);
                                FCGridCell cell16 = row.getCell(columnIndex.get(10));
                                cell16.setDouble(priceData.m_flow);
                                FCGridCell cell17 = row.getCell(columnIndex.get(11));
                                if (DataCenter.m_securityService.m_securitiesMap.containsKey(code))
                                {
                                    cell17.setString(DataCenter.m_securityService.m_securitiesMap.get(code).m_block);
                                }
                                FCGridCell cell18 = row.getCell(columnIndex.get(12));
                                cell18.setDouble(priceData.m_upperLimit);
                                cell18.m_style.m_textColor = FCDraw.getPriceColor(priceData.m_upperLimit, lastClose);
                                FCGridCell cell19 = row.getCell(columnIndex.get(13));
                                cell19.setDouble(priceData.m_lowerLimit);
                                cell19.m_style.m_textColor = FCDraw.getPriceColor(priceData.m_lowerLimit, lastClose);

                            }
                        }
                        invalidate();
                    }
                }
            }
        }
        else
        {
            ArrayList<Security> securities = new ArrayList<Security>();
            for (int i = 0; i < m_rows.size(); i++)
            {
                FCGridRow row = m_rows.get(i);
                String sCode = row.getTag().toString();
                if (DataCenter.m_securityService.m_securitiesMap.containsKey(sCode))
                {
                    securities.add(DataCenter.m_securityService.m_securitiesMap.get(sCode));
                }
            }
            clearRows();
            update();
            invalidate();
            searchCodes(securities);
        }
    }

    /*
     * 回调方法
     *@param sender
     *@param message
     *@param invoke
     */
    public void callListenerMessageEvent(Object sender, FCMessage message, Object invoke)
    {
        this.invoke(message);
    }

    public void delete()
    {
        if (!isDeleted())
        {
            DataCenter.m_priceDataServiceClient.removeListener(m_requestID);
            DataCenter.m_priceDataServiceClient.unSubCodes(m_requestID);
        }
        super.delete();
    }

    public boolean m_clickLeftOrRight;

    public void onTouchDown(FCTouchInfo touchInfo)
    {
        callTouchEvents("ontouchdown", touchInfo);
        if (!allowPreviewsEvent())
        {
//            onDragScrollStart();
        }
        m_clickLeftOrRight = touchInfo.m_firstTouch;
        touchEvent(touchInfo, 1);
    }

    public boolean m_firstAdd = true;
    public void onAdd()
    {
        super.onAdd();
        if (m_firstAdd)
        {
            m_firstAdd = false;
            addEvent(this, "oninvoke", this);
        }
    }

    public void searchCodes(ArrayList<Security> securities)
    {
        if (m_requestID != -1)
        {
            DataCenter.m_priceDataServiceClient.removeListener(m_requestID);
            DataCenter.m_priceDataServiceClient.unSubCodes(m_requestID);
        }
        bindCodes(securities);
        m_requestID = FCClientService.getRequestID();
        DataCenter.m_priceDataServiceClient.addListener(m_requestID, this, this);
        StringBuilder sb = new StringBuilder();
        for (Security security : securities)
        {
            sb.append(security.m_code + ",");
        }
        String codes = sb.toString();
        if (codes.endsWith(","))
        {
            codes = codes.substring(0, codes.length() - 1);
        }
        DataCenter.m_priceDataServiceClient.subCodes(m_requestID, codes);
    }

    /*
     * 第一次搜索代码
     */
    public void firstSearchCode()
    {
        ArrayList<Security> securities = new ArrayList<Security>();
        for (Security security : DataCenter.m_securityService.m_securities)
        {
            if (security.m_type != 0)
            {
                securities.add(security);
            }
        }
        searchCodes(securities);
    }

    public void onSelectedRowsChanged()
    {
        super.onSelectedRowsChanged();
        try
        {
            if (m_clickLeftOrRight)
            {
                if (getSelectedRows().get(0).m_cells.size() > 0)
                {
                    String code = getSelectedRows().get(0).getTag().toString();
                    MainFrame.m_mainFrame.searchCode(code);
                }
            }
        }
        catch (Exception ex)
        {

        }
    }
}

